<?php

require_once("database.inc");
require_once("getsets.inc");

session_start();

if (!validateUser())
{	// validation falied
	// show user login and 
	exit; // halt processing
}

showUserLogout();

/*
* Returns true if user login is valid, false otherwise.
*/
function validateUser()
{
	$email = getFormVariable("email");
	$password = getFormVariable("password");
	$lastLoginFailed = getSessionVariable("lastLoginFailed");
	
	if (getFormVariable("forceLogout"))
	{
		setSessionVariable("userId", 0);
		setSessionVariable("lastLoginFailed", false);
		showUserLogin(false);
		return false;
	}
	
	if (getSessionVariable("userId"))
	{
		return true; // already logged in
	}

	$userId = sqlSelectUserId($email, $password);
	
	if (!$userId)
	{
		showUserLogin($lastLoginFailed);
		setSessionVariable("lastLoginFailed", true);
		return false;
	}
	
	// set successful login info
	setSessionVariable("userId", $userId);

	return true;
}

/*
* Show user login form.
*/
function showUserLogin($lastLoginFailed)
{
	?>
	<html>
	<head>
	<title>UNCW DDAS Project Researcher Login</title>
	<style type="text/css">
		a {color: #009999;}
		h1 {font-size: 120%; font-weight: normal; color: #009999;}
		p {font-size: 80%;}
	</style>
	</head>
	<body style="margin: 0px; font-family: arial;">
	<table width="729px">
		<tr>
			<td style="padding-bottom: 10px;">
				<img src="http://www.uncw.edu/www/images/UNCWmasthead.gif">
			</td>
		</tr>
		<tr>
			<td style="padding: 5px;">
				<h1>
					UNCW Distributed Data Analysis System (DDAS)
				</h1>
				
				<table>
					<tr>
						<td valign="top">
							<p>
								Welcome to the problem submission system of UNCW DDAS, an implementation of a Virtual Campus Supercomputer Center (VCSC) using the Berkeley Open Infrastructure for Network Computing (BOINC).
							</p>
							<p>
								Access to this system is limited and requires an account, which may be obtained by contacting Dr. Douglas Kline at <a href="douglaskline@gmail.com">douglaskline@gmail.com</a>.
							</p>
						</td>
						<td width="10px"></td>
						<td>
							<form name="login" method="post" action="<?php echo($_SERVER["PHP_SELF"]); ?>">
							<table cellpadding="5px" style="border: 1px solid #666666;">
								<tr>
									<td colspan="3" style="background-color: #cccccc; text-align: center;">
										Researcher Login
									</td>
								</tr>
								<tr>
									<td>Email:</td>
									<td colspan="2"><input type="text" name="email" maxlength="50" size="30"></td>
								</tr>
								<tr>
									<td>Password:</td>
									<td><input type="password" name="password" maxlength="25" size="20"></td>
									<td align="left"><input type="submit" value="Login"></td>
								</tr>
							</table>
							</form>
							<?php
							if ($lastLoginFailed)
							{
								echo("<div style='color: #ff0000; font-size: 11pt;'>Invalid Email Address or Password</div>");
							}
							?>
						</td>
					</tr>
				</table>

			</td>
		</tr>
	</table>
	<?php
}

/*
* Show user logout form.
*/
function showUserLogout()
{
	?>
	<div style="position: absolute; top: 10px; left: 660px;">
		<form action="index.php" method="POST">
			<input type="hidden" name="forceLogout" value="true">
			<input type="submit" value="Logout">
		</form>
	</div>
	<?php
}

?>